package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.InsuranceBean;
import com.oscar.ehome.bean.pagebean.WorkLogBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.InsuranceAdapter;
import com.oscar.ehome.view.pageview.adapter.WorkLogAdapter;
import com.oscar.ehome.view.pageview.view.InuranceListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/4
 */

public class WorkListActivity extends BasesActivity<InuranceListView, PagePresenter> implements PullToRefreshBase.OnRefreshListener,
        AdapterView.OnItemClickListener, ProgressFrameLayout.AgainRequestListener, InuranceListView {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 添加
     */
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    /**
     * 添加
     */
    @BindView(R.id.title_right_layput)
    RelativeLayout rightLayout;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * list view
     */
    @BindView(R.id.worklog_fragment_listview)
    PullToRefreshListView worklogFragmentListview;
    /**
     * 无网络布局
     */
    @BindView(R.id.worklog_fragment)
    ProgressFrameLayout worklogFragment;

    WorkLogAdapter adapter;
    private List<WorkLogBean.ListWorklogBean> list = new ArrayList<>();
    PagePresenter presenter;
    String token = "";
    String id = "";
    int page=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_worklog);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(WorkListActivity.this).getSystemConfig().getColor()));
        initView();
        onClick();
    }

    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        rightLayout.setVisibility(View.VISIBLE);
        titleImgRight.setImageResource(R.mipmap.jia);
        titleTopbar.setText("工作日志");

        token = SystemConfigFactory.getInstance(WorkListActivity.this).getSystemConfig().getToken();

        adapter = new WorkLogAdapter(WorkListActivity.this, list);
        worklogFragmentListview.setAdapter(adapter);
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            worklogFragment.stopRefresh();
            worklogFragmentListview.setVisibility(View.VISIBLE);
            String s = String.valueOf(page);
            presenter.getWorkList(token,"20", s);
            showLoading();
        } else {
            worklogFragment.setVisibility(View.VISIBLE);
            worklogFragment.setLoadFail();
            worklogFragmentListview.setVisibility(View.GONE);
        }
    }

    public void onClick() {
        worklogFragmentListview.setPullLoadEnabled(true);
        worklogFragmentListview.setOnRefreshListener(this);
        worklogFragmentListview.setOnItemClickListener(this);
        worklogFragment.setAgainRequestListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (list.size() > 0) {
            if (list.get(position).getId() != null) {
                Intent intent = new Intent(this, WorkLogActivity.class);
                intent.putExtra("id", list.get(position).getId());
                startActivity(intent);
            }
        }
    }

    @Override
    public void getInsuranceList(InsuranceBean result) {

    }

    @Override
    public void getWorkList(WorkLogBean result) {
        dismissLoading();
        worklogFragmentListview.onPullDownRefreshComplete();
        worklogFragmentListview.onPullUpRefreshComplete();
        if (result != null) {
            list = new ArrayList<>();
            list = result.getList();
            adapter.setLists(list);
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        worklogFragmentListview.onPullDownRefreshComplete();
        worklogFragmentListview.onPullUpRefreshComplete();
        ToastUtil.show(this, getString(R.string.erry));
        worklogFragment.setLoadFail();
        worklogFragmentListview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        worklogFragmentListview.onPullDownRefreshComplete();
        worklogFragmentListview.onPullUpRefreshComplete();
        ToastUtil.show(this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            worklogFragment.setNoResult();
            worklogFragmentListview.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        String s = String.valueOf(page);
        presenter.getWorkList(token, "20", s);
        showLoading();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        String s = String.valueOf(page);
        presenter.getWorkList(token, "20", s);
        showLoading();
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(this, R.string.pull_to_refresh_network_error);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @OnClick({R.id.title_layout_left, R.id.title_right_layput})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_layout_left:
                finish();
                break;
            case R.id.title_right_layput:
                Intent intent = new Intent(WorkListActivity.this, WorkLogActivity.class);
                intent.putExtra("id", "");
                startActivity(intent);
                break;
        }
    }
}
